
import Express from "express"
import { MessageServers } from '../../server/message'

const RouterMessage = Express.Router();

RouterMessage.post('/', async (req, res) => {
    const addRes = await MessageServers.add(req.body);

    if(Array.isArray(addRes)) {
        const errData = addRes.join(';')
        res.send({
            err: errData,
            data: '',
            status: "失败"
        })
    } else {
        res.send({
            err: '',
            data: addRes,
            status: "成功"
        })
    }
})

RouterMessage.delete('/:id', async (req, res) => {
    const deleteData = await MessageServers.delete(req.params.id);
    res.send({
        data: deleteData,
        status: `删除了${deleteData.deletedCount}条数据`
    })
})

// RouterMessage.put('/:id', async (req, res) => {
    // const editData = await MessageServers.edit(req.params.id, req.body)
    // res.send({
        // data: editData
    // })
// })


// RouterMessage.get('/:id', async (req, res) => {
    // const findOne = await MessageServers.find(req.params.id);
    // res.send({
        // data: findOne,
        // status: 'success'
    // })
// })

RouterMessage.get('/', async (req, res) => {
    const queryData = await MessageServers.find()

    res.send(queryData)
})


export { RouterMessage }